﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using ClsSdkXX = LabelPrintSystem.ClsZebraSdk_BTActiveXCOM;
using System.Text.RegularExpressions;

namespace LabelPrintSystem
{
    internal class ClsProcess
    {
        #region event
        public event EventHandler evt_show_log_handle;
        protected void On_evt_show_log_handle(object obj, EventArgs e)
        {
            if (this.evt_show_log_handle != null)
                this.evt_show_log_handle(obj, e);
        }
        public event EventHandler evt_get_engine_handle;
        protected void On_evt_get_engine_handle(object obj, EventArgs e)
        {
            if (this.evt_get_engine_handle != null)
                this.evt_get_engine_handle(obj, e);
        }
        #endregion
        //是否已加载bartender IO
        public bool BlGetEngine { get; set; }
        public virtual void getPrinters() { }
        public virtual bool getEngine() { return false; }
        public virtual void StartProcess() { }
        public virtual void LoadFormat(int id) { }
        public virtual void StopProcess() { }
        public virtual bool ToSettingSN(string SN) { return false; }
        public virtual void EngineVisible(bool isVisible, int SelectIndex) { }
        public virtual void OpenLabelFormat(int SelectIndex) { }
    }

    class ClsProcessForMOD : ClsProcess
    {
        private ClsDataProduct ClsDataProductParam;
        private ClsSdkXX ClsSdk;
        private ClsPrintInfo PrintInfo;
        private ClsResultForMOD ClsResult;
        private ClsDataUser ClsUser;
        private string ErrMsg;

        public ClsProcessForMOD(ClsPrintInfo PrintInfoParam, ClsDataUser ClsUser)
        {
            ClsResult = new ClsResultForMOD(ClsUser.sEquID);
            this.ClsSdk = new ClsSdkXX(this.PrintInfo = PrintInfoParam);
            this.ClsDataProductParam = new ClsDataProduct();
            this.ClsUser = ClsUser;
            evt_book();
        }

        private void evt_book()
        {
            ClsSdk.evt_show_log_handle += ClsSdk_evt_show_log_handle;
            ClsSdk.evt_get_engine_handle += ClsSdk_evt_get_engine_handle;
            ClsSdk.evt_complete_LoadFormat_handle += ClsSdk_evt_complete_LoadFormat_handle;
            ClsSdk.evt_complete_print_handle += ClsSdk_evt_complete_print_handle;
        }
        void ClsSdk_evt_get_engine_handle(object sender, EventArgs e)
        {
            base.On_evt_get_engine_handle(sender, e);
        }
        void ClsSdk_evt_complete_LoadFormat_handle(object sender, EventArgs e)
        {
            this.PrintInfo.FormatSubStringsChange = new Dictionary<string, string>();
            ClsPrintInfo info = sender as ClsPrintInfo;
            if (info.FormatSubStrings == null)
            {
                ErrMsg = "FormatSubStrings = null,模板设置有问题,无法自动更新条码";
                ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_err });
                return;
            }
            if (!info.FormatSubStrings.ContainsKey(ClsDifinition.keys1ForMOD))
            {
                ErrMsg = string.Format("FormatSubStrings 不存在{0},无法自动更新条码(1P)", ClsDifinition.keys1ForMOD);
                ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_err });
                return;
            }
            else
            {
                this.PrintInfo.FormatSubStringsChange.Add(ClsDifinition.keys1ForMOD, this.ClsDataProductParam.sClientProcode);
            }
            if (!info.FormatSubStrings.ContainsKey(ClsDifinition.keys2ForMOD))
            {
                ErrMsg = string.Format("FormatSubStrings 不存在{0},无法自动更新条码(G9)", ClsDifinition.keys2ForMOD);
                ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_err });
                return;
            }
            else
            {
                this.PrintInfo.FormatSubStringsChange.Add(ClsDifinition.keys2ForMOD, this.ClsDataProductParam.sProductSN);
            }
            //开始打印  
            if (CheckProductStep(this.ClsDataProductParam.sProductSN))
            {//检测上道工序
                ClsSdk_evt_show_log_handle("检测上道工序已完成", new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_log });
                this.ClsSdk.PrintLabel();
                if (this.ClsDataProductParam.sProcode == "STR2200090")
                {
                    this.PrintInfo.FormatSubStringsChange[ClsDifinition.keys1ForMOD] = this.ClsDataProductParam.sClientProcode.Insert(2, "NG");
                    this.ClsSdk.PrintLabel();
                }
            }
            else
            {
                ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_err });
            }
        }
        void ClsSdk_evt_show_log_handle(object sender, EventArgs e)
        {
            base.On_evt_show_log_handle(sender, e);
        }
        void ClsSdk_evt_complete_print_handle(object sender, EventArgs e)
        {
            if (!UpData(this.ClsDataProductParam.sProductSN, this.ClsUser.sUserID, this.ClsUser.sEquID))
            {
                ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_err });
            }
            else
            {
                ClsSdk_evt_show_log_handle("上传数据成功", new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_complete });
            }
        }
        public override void getPrinters()
        {
            //base.getPrinters();
            this.ClsSdk.getPrinters();
        }
        public override bool getEngine()
        {
            //return base.getEngine();
            return BlGetEngine = this.ClsSdk.iniEngine();
        }
        public override bool ToSettingSN(string SN)
        {
            if (SN.Length > 35)
            {//二维码：[)>061P089024A.101SG91732D009EQ118VLENSN4LCN10D1732  --->注意里面含有二维码编码转义字符  VS2013看不到，VS2015才能看到(notepad++也可以看到)  临时处理抓取字符串时候避开转义字符
                //"[)>\u001e06\u001d1P089024A.101\u001dSG91732D009E\u001dQ1\u001d18VLENSN\u001d4LCN\u001d10D1732\u001e\u0004"
                //条码枪扫描出来的二维码会自动删除转义字符    [)>061P089024A.101SG91732D009EQ118VLENSN4LCN10D1732 
                //[)>061P086659A.101SG91802AB3EA118VLENSN4LCN10D1732
                this.ClsDataProductParam.sClientProcode = SN.Substring(5, 13);
                this.ClsDataProductParam.sProductSN = SN.Substring(18, 12);
            }
            else
            {
                this.ClsDataProductParam.sProductSN = SN;//成品条码：SG91732D009E                      
                FrmClientProcode frm = new FrmClientProcode();
                if (frm.ShowDialog() != DialogResult.OK)
                {
                    return false;
                }
                this.ClsDataProductParam.sClientProcode = frm.textProductID.Text.Trim().ToUpper();//客户成品代码3BK:1P089024A.101
            }
            string str = string.Format("成品序列号：{0}\r\n客户成品代码：{1}", this.ClsDataProductParam.sProductSN, this.ClsDataProductParam.sClientProcode);
            ClsSdk_evt_show_log_handle(str, new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_log });
            return true;
        }
        public override void StartProcess()
        {
            // base.StartProcess();
            if ((this.ClsDataProductParam.sProcode = MathProductSN(this.ClsDataProductParam.sProductSN)) == null)
            {//匹配产品代码
                ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_err });
                return;
            }
            ClsSdk_evt_show_log_handle(string.Format("产品代码:{0}", this.ClsDataProductParam.sProcode), new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_log });

            if (!GetTemple(this.ClsDataProductParam.sProcode))
            {//获取标签模板
                ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_err });
                return;
            }
            else
            {
                ClsSdk_evt_show_log_handle(this.PrintInfo.browsingFormats, new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_get_temple });
                if (this.PrintInfo.browsingFormats.Length > 1)
                {
                    ErrMsg = "标签模板不唯一，无法自动打印";
                    ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_err });
                    return;
                }
            }
            LoadFormat();
        }
        public override void LoadFormat(int id = 0)
        {
            //base.LoadFormat(id);
            this.PrintInfo.SelectFormatName = this.PrintInfo.browsingFormats[id];
            ClsSdk_evt_show_log_handle(string.Format("FormatName:{0}", this.PrintInfo.SelectFormatName), new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_log });
            //打印
            this.ClsSdk.FormatLoader();
        }
        public override void StopProcess()
        {
            //base.StopProcess();
            this.ClsSdk.StopPrint();
        }

        private string MathProductSN(string sProductSN)
        {
            string SN = string.Empty;
            SN = ClsResult.GetProductTypeID_byPackageProductSN(sProductSN);
            if (SN == string.Empty)
            {
                ErrMsg = "无法匹配产品代码";
                return null;
            }
            return SN;
        }

        private bool CheckProductStep(string sProductSN)
        {
            bool value = false;
            value = ClsResult.HasValid_PackageData(sProductSN);
            if (!value)
            {
                ErrMsg = "该产品上道工序(关联、包装)未完成";
            }
            return value;
        }

        private bool UpData(string packageProductSN, string userID, string pCIP)
        {
            if (ClsDifinition.isDebug) return true;
            bool value = false;
            value = ClsResult.DataUpload(packageProductSN, userID, pCIP);
            if (!value)
            {
                ErrMsg = "上传数据失败";
            }
            return value;
        }

        private bool GetTemple(string sProcode)
        {//查找文件路径模板  代入S代码
            if ((this.PrintInfo.browsingFormats = getTemple(sProcode)) == null)
                return false;
            else
                return true;
        }
        private string[] getTemple(string sProcode)
        {//代入S代码查询标签模板
            string sPath = this.PrintInfo.sTemplePath;// @"D:\天线标签追溯系统项目\LabelTemple\";
            try
            {
                string[] browsingFormats = System.IO.Directory.GetFiles(sPath, "*.btw");
                List<string> LsbrowsingFormats = new List<string>();

                foreach (string obj in browsingFormats)
                {
                    string sobj = System.IO.Path.GetFileName(obj);
                    if (sobj.Contains(sProcode))
                    {
                        LsbrowsingFormats.Add(obj);
                    }
                }
                if (LsbrowsingFormats.Count <= 0)
                {
                    ErrMsg = string.Format("未能找到{0}对应模板,请建立好模板以{1}命名并存放到{2}目录下", sProcode, this.ClsDataProductParam.sProcode, sPath);
                    return null;
                }
                return LsbrowsingFormats.ToArray();
            }
            catch (Exception ex)
            {
                ErrMsg = ex.ToString();
            }
            return null;
        }

    }

    class ClsProcessForANTENNA : ClsProcess
    {
        object m_handle;
        private ClsDataProduct ClsDataProductParam;//天线产品信息
        private ClsSdkXX ClsSdk;
        private ClsPrintInfo PrintInfo;
        private ClsDataUser ClsUser;
        private ClsServicePIMAndElecForAntenna ClsPIMAndElec;
        private clsProcessForCheckAntennaTest m_ProcessForCheckAntennaTest;
        private string ErrMsg;
        public ClsProcessForANTENNA(object handle, ClsPrintInfo pinfo, ClsDataUser ClsUser)
        {
            this.m_handle = handle;
            this.ClsPIMAndElec = new ClsServicePIMAndElecForAntenna();
            this.ClsDataProductParam = new ClsDataProduct();
            this.PrintInfo = pinfo;
            this.ClsUser = ClsUser;
            this.ClsSdk = new ClsSdkXX(this.PrintInfo);
            evt_book();
            m_ProcessForCheckAntennaTest = new clsProcessForCheckAntennaTest();
            m_ProcessForCheckAntennaTest.ToSetConfig(this.PrintInfo.config);
        }

        void evt_book()
        {
            ClsSdk.evt_show_log_handle += ClsSdk_evt_show_log_handle;
            ClsSdk.evt_get_engine_handle += ClsSdk_evt_get_engine_handle;
            ClsSdk.evt_complete_LoadFormat_handle += ClsSdk_evt_complete_LoadFormat_handle;
            ClsSdk.evt_complete_print_handle += ClsSdk_evt_complete_print_handle;
        }
        void ClsSdk_evt_get_engine_handle(object sender, EventArgs e)
        {
            base.On_evt_get_engine_handle(sender, e);
        }
        void ClsSdk_evt_complete_LoadFormat_handle(object sender, EventArgs e)
        {
            string sn = this.ClsDataProductParam.sProductSN;
            if (PrintInfo.config.isNokia)
            {
                string prefix = sn.Substring(0, 2).ToUpper();
                if (prefix == "JA" || prefix == "XA" || prefix == "MB")
                {
                    sn = sn.Replace(prefix, PrintInfo.config.sNokia);
                }
            }
            this.PrintInfo.FormatSubStringsChange = new Dictionary<string, string>();
            ClsPrintInfo info = sender as ClsPrintInfo;
            if (info.FormatSubStrings == null)
            {
                ErrMsg = "数据关联(共享/名称)为空,模板设置有问题,无法自动更新条码,请打开相应模板设置变量名";
                ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_err });
                update_ui_status();
                return;
            }
            if (!info.FormatSubStrings.ContainsKey(ClsDifinition.key1ForANTENNA))
            {
                ErrMsg = string.Format("数据关联(共享/名称) 不存在{0},无法自动更新条码,请按照约定将相应模板的条码设置变量名为:{0}", ClsDifinition.key1ForANTENNA);
                ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_err });
                update_ui_status();
                return;
            }
            else
            {
                if (!PrintInfo.config.isSpecial_R_AAU)
                {
                    this.PrintInfo.FormatSubStringsChange.Add(ClsDifinition.key1ForANTENNA, sn);
                }
                else
                {
                    this.PrintInfo.FormatSubStringsChange.Add(ClsDifinition.key1ForANTENNA, this.ClsDataProductParam.sProductSN_R_AAU);
                }
            }
            if (!info.FormatSubStrings.ContainsKey(ClsDifinition.key2ForANTENNA))
            {
                if (!PrintInfo.config.isCheckQrCode)
                {
                    ErrMsg = string.Format("数据关联(共享/名称) 不存在{0},无法自动更新模板里面二维码的信息！请确认模板是否含有二维码,如不含请单击继续;如含有需要自动更新,请取消,先按照约定将相应模板的条码设置变量名为:{0}", ClsDifinition.key2ForANTENNA);
                    if (!(m_handle as FrmMain).Tips(ErrMsg + "是否继续？"))
                    {
                        ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode(ClsEventMode.en_evtMode.evt_complete));
                        update_ui_status();
                        return;
                    }
                }
            }
            else
            {
                string barcode = info.FormatSubStrings[ClsDifinition.key1ForANTENNA];
                string QRcode = info.FormatSubStrings[ClsDifinition.key2ForANTENNA];
                if (!QRcode.Contains(barcode))
                {
                    ErrMsg = string.Format("模板中二维码信息与条码信息不匹配,无法自动更新二维码！按照约定标签模板二维码应该包含条码的信息,请修改好相应标签模板", ClsDifinition.key2ForANTENNA);
                    ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_err });
                    update_ui_status();
                    return;
                }
                if (!PrintInfo.config.isSpecial_R_AAU)
                {
                    QRcode = QRcode.Replace(barcode, sn);
                }
                else
                {
                    QRcode = QRcode.Replace(barcode, this.ClsDataProductParam.sProductSN_R_AAU);
                }
                this.PrintInfo.FormatSubStringsChange.Add(ClsDifinition.key2ForANTENNA, QRcode);
            }
            //开始打印           
            if (/*TestData_Process()*/m_ProcessForCheckAntennaTest.Process()) this.ClsSdk.PrintLabel();
        }
        void ClsSdk_evt_show_log_handle(object sender, EventArgs e)
        {
            base.On_evt_show_log_handle(sender, e);
        }
        void ClsSdk_evt_complete_print_handle(object sender, EventArgs e)
        {
            PrintRecordInfo record = new PrintRecordInfo();
            record.ProductSN = this.ClsDataProductParam.sProductSN;
            record.PCIP = this.ClsUser.sEquID;
            record.RecordTime = DateTime.Now;
            record.UserId = this.ClsUser.sUserID;
            record.TempName = System.IO.Path.GetFileName(this.PrintInfo.SelectFormatName);
            if (PrintInfo.config.isSpecial_R_AAU)
            {
                record.Remark = this.ClsDataProductParam.sProductSN_R_AAU;
            }
            else if (PrintInfo.config.isNokia)
            {
                record.Remark = "Nokia";
            }
            else
            {
                record.Remark = "";
            }

            if (!WritePrintRecord(record))
            {
                ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_err });
            }
            else
            {
                ClsSdk_evt_show_log_handle("上传数据成功", new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_complete });
            }
            update_ui_status();
        }

        public override bool getEngine()
        {
            //return base.getEngine();
            return this.ClsSdk.iniEngine();
        }
        public override void getPrinters()
        {
            //base.getPrinters();
            this.ClsSdk.getPrinters();
        }
        public override bool ToSettingSN(string SN)
        {
            //return base.ToSettingSN(SN);
            this.ClsDataProductParam.sProductSN = SN;
            if (PrintInfo.config.isSpecial_R_AAU)
            {
                FrmClientProcode frm = new FrmClientProcode() { LblText = "请输入模板需要更新的条码" };
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    this.ClsDataProductParam.sProductSN_R_AAU = frm.textProductID.Text.Trim();
                    ClsSdk_evt_show_log_handle(string.Format("特殊产品,测试与出货条码不一致!-->模板条码更新为:{0}", this.ClsDataProductParam.sProductSN_R_AAU), new ClsEventMode());
                }
                else { this.ClsDataProductParam.sProductSN_R_AAU = string.Empty; return false; }
            }
            return true;
        }
        public override void StartProcess()
        {
            if (this.PrintInfo.config.setModule == (int)ClsConfig.SetModule.reform)
            {//改造产品数据库无法追溯产品代码(数据库没有设置关联)，直接拆分条码
                if (!getProcode())
                {//获取产品代码
                    return;
                }
                if (!isGetTemple())
                {//获取标签模板
                    return;
                }
            }
            else
            {//免测 & 常规 & 重复打印
                if (ReadPrintRecord())
                {//重复打印--提示
                    if (!(m_handle as FrmMain).Tips("该条码已打印过,是否继续？"))
                    {
                        ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_complete });
                        return;
                    }
                }

                if (!m_ProcessForCheckAntennaTest.ToSetProductParam(ClsDataProductParam))
                {
                    ClsSdk_evt_show_log_handle(m_ProcessForCheckAntennaTest.m_Msg, new ClsEventMode(ClsEventMode.en_evtMode.evt_err));
                    return;
                }
                else
                {
                    ClsSdk_evt_show_log_handle(string.Format("产品序列号:{0}\r\n产品代码:{1}\r\n批号MOID:{2}\r\n类型名称:{3}",
                   this.ClsDataProductParam.sProductSN, this.ClsDataProductParam.sProcode, this.ClsDataProductParam.sMOID, this.ClsDataProductParam.sProcodeName), new ClsEventMode());
                    if (!isGetTemple())
                    {//获取标签模板
                        return;
                    }
                }
                //if (!ReadMatchSN(this.ClsDataProductParam))
                //{
                //    ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode(ClsEventMode.en_evtMode.evt_err));
                //    return;
                //}
                //else
                //{
                //    ClsSdk_evt_show_log_handle(string.Format("产品序列号:{0}\r\n产品代码:{1}\r\n批号MOID:{2}\r\n类型名称:{3}",
                //     this.ClsDataProductParam.sProductSN, this.ClsDataProductParam.sProcode, this.ClsDataProductParam.sMOID, this.ClsDataProductParam.sProcodeName), new ClsEventMode());
                //    if (!isGetTemple())
                //    {//获取标签模板
                //        return;
                //    }                    
                //}
            }
            LoadFormat();
        }
        public override void StopProcess()
        {
            //base.StopProcess();
            this.ClsSdk.StopPrint();
        }
        public override void LoadFormat(int id = 0)
        {
            //base.LoadFormat(id);
            this.PrintInfo.SelectFormatName = this.PrintInfo.browsingFormats[id];
            ClsSdk_evt_show_log_handle(string.Format("标签模板:{0}", this.PrintInfo.SelectFormatName), new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_log });
            //打印
            this.ClsSdk.FormatLoader();
        }
        public override void EngineVisible(bool isVisible, int SelectIndex)
        {
            if (SelectIndex == -1)
            {
                this.ClsSdk.EngineVisible(isVisible, null);
            }
            else
            {
                this.ClsSdk.EngineVisible(isVisible, this.PrintInfo.SelectFormatName = this.PrintInfo.browsingFormats[SelectIndex]);
            }
        }
        public override void OpenLabelFormat(int SelectIndex)
        {
            //base.OpenLabelFormat(SelectIndex);
            ClsSdk.OpenFormatProcsss(this.PrintInfo.SelectFormatName = this.PrintInfo.browsingFormats[SelectIndex]);
        }


        void update_ui_status()
        {
            string lbstatus = "";
            if (PrintInfo.config.setModule == (int)ClsConfig.SetModule.normal)
            {
                lbstatus = "类型:常规产品";
            }
            else if (PrintInfo.config.setModule == (int)ClsConfig.SetModule.reform)
            {
                lbstatus = string.Format("类型:改造产品,改造数量:{0}", PrintInfo.config.numReform);
            }
            else if (PrintInfo.config.setModule == (int)ClsConfig.SetModule.uCheck)
            {
                lbstatus = string.Format("类型:免测产品,交调数量:{0},电性能数量{1},RCU数量{2}", PrintInfo.config.numPIM, PrintInfo.config.numElectric, PrintInfo.config.numRcu);
            }
            ClsSdk_evt_show_log_handle(lbstatus, new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_update_lb });
        }
        bool isGetTemple()
        {
            if (!GetTemple(this.ClsDataProductParam.sProcode))
            {//获取标签模板
                ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_err });
                return false;
            }
            else
            {
                ClsSdk_evt_show_log_handle(this.PrintInfo.browsingFormats, new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_get_temple });
                if (this.PrintInfo.browsingFormats.Length > 1)
                {
                    ErrMsg = "标签模板不唯一,无法自动打印！请选中相应模板后点击手动打印.";
                    ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_err });
                    return false;
                }
            }
            return true;
        }
        bool getProcode()
        {
            if (this.ClsDataProductParam.sProductSN.Length < 12)
            {
                ErrMsg = "输入条码格式不正确";
                ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode() { evt_mode = ClsEventMode.en_evtMode.evt_err });
                return false;
            }
            this.ClsDataProductParam.sProcode = this.ClsDataProductParam.sProductSN.Substring(2, 10);
            ClsSdk_evt_show_log_handle(string.Format("产品序列号:{0}\r\n产品代码:{1}",
                    this.ClsDataProductParam.sProductSN, this.ClsDataProductParam.sProcode), new ClsEventMode());
            return true;
        }


        bool TestData_Process()
        {
            if (this.PrintInfo.config.setModule == (int)ClsConfig.SetModule.reform)
            {
                if (!Reform_Process())
                {//改造流程
                    return false;
                }
            }
            else if (this.PrintInfo.config.setModule == (int)ClsConfig.SetModule.uCheck)
            {
                if (!UnCheck_Process())
                {//免测流程
                    return false;
                }
            }
            else if (this.PrintInfo.config.setModule == (int)ClsConfig.SetModule.normal)
            {
                if (!Normal_Process())
                {//常规流程
                    return false;
                }
            }
            return true;
        }
        bool Reform_Process()
        {
            bool value = false;
            if (this.PrintInfo.config.numReform-- > 0)
            {
                value = true;
            }
            if (this.PrintInfo.config.numReform <= 0)
            {//改造复位
                this.PrintInfo.config.setModule = (int)ClsConfig.SetModule.normal;
            }
            return value;
        }
        bool UnCheck_Process()
        {
            bool value = false;

            bool isuPIM = this.PrintInfo.config.isuPIM,
                 isuElectric = this.PrintInfo.config.isuElectric,
                 isuRcu = this.PrintInfo.config.isuRcu;
            int numPIM = this.PrintInfo.config.numPIM,
                numElectric = this.PrintInfo.config.numElectric,
                numRcu = this.PrintInfo.config.numRcu;

            if (!isuPIM)
            {
                if (!ReadPimIsPass())
                {
                    ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode(ClsEventMode.en_evtMode.evt_err));
                    return value;
                }
                ClsSdk_evt_show_log_handle("交调测试合格", new ClsEventMode());
            }
            else
            {
                if (--numPIM <= 0)
                {//交调免测复位
                    isuPIM = false;
                }
            }

            if (!isuElectric)
            {
                if (!ReadElectricIsPass())
                {
                    ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode(ClsEventMode.en_evtMode.evt_err));
                    return value;
                }
                ClsSdk_evt_show_log_handle("电性能测试合格", new ClsEventMode());
            }
            else
            {
                if (--numElectric <= 0)
                {//电性能免测复位
                    isuElectric = false;
                }
            }

            if (!isuRcu)
            {
                if (!ReadRcuIsPass())
                {
                    ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode(ClsEventMode.en_evtMode.evt_err));
                    return value;
                }
                ClsSdk_evt_show_log_handle("RCU检测合格", new ClsEventMode());
            }
            else
            {
                if (--numRcu <= 0)
                {//RCU免测复位
                    isuRcu = false;
                }
            }

            this.PrintInfo.config.isuPIM = isuPIM;
            this.PrintInfo.config.isuElectric = isuElectric;
            this.PrintInfo.config.isuRcu = isuRcu;
            this.PrintInfo.config.numPIM = numPIM;
            this.PrintInfo.config.numElectric = numElectric;
            this.PrintInfo.config.numRcu = numRcu;
            if (this.PrintInfo.config.isuPIM == false && this.PrintInfo.config.isuPIM == this.PrintInfo.config.isuElectric && this.PrintInfo.config.isuPIM == this.PrintInfo.config.isuRcu)
            {
                this.PrintInfo.config.setModule = (int)ClsConfig.SetModule.normal;
            }
            return value == false;
        }
        bool Normal_Process()
        {
            bool value = false;
            if (!ReadPimIsPass())
            {
                ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode(ClsEventMode.en_evtMode.evt_err));
                return value;
            }
            ClsSdk_evt_show_log_handle("交调测试合格", new ClsEventMode());
            if (!ReadElectricIsPass())
            {
                ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode(ClsEventMode.en_evtMode.evt_err));
                return value;
            }
            ClsSdk_evt_show_log_handle("电性能合格", new ClsEventMode());
            if (!ReadRcuIsPass())
            {
                ClsSdk_evt_show_log_handle(ErrMsg, new ClsEventMode(ClsEventMode.en_evtMode.evt_err));
                return value;
            }
            ClsSdk_evt_show_log_handle("RCU检测合格", new ClsEventMode());
            return value == false;
        }
        bool ReadMatchSN(ClsDataProduct DataProduct)
        {/*
          *1、select MOID from THouse where ProductSN ='ProductSN'; 产品批次MOID  不存在 retrun false;
          *2、select ProductTypeID from TMO where MOID = 'xx';  产品代码 不存在 retrun false;
          *3、select ProductTypeName from TProductType where ProductTypeID = 'xx';  产品名称 不存在 retrun false;
          *4、都存在 retrun true and MOID  and ProductTypeID and  ProductTypeName
          */

            //A、匹配产品参数接口：
            //1、ProductMOID_GetbySN----通过SN，获取SN对应的批号MOID，
            //输入参数(string)---产品条码ProductSN  输出参数(string)---MOID
            //2、ProductTypeID _GetbyMOID----通过MOID，获取MOID对应的产品代码(S代码)，输入参数(string)--- MOID  输出参数(string)---S代码
            //3、ProductTypeName_GetbyProductTypeID---通过S代码，获取S代码对应的产品类型名称，输入参数(string)---S代码，输出参数(string)---产品类型名称

            if ((this.ClsDataProductParam.sMOID = this.ClsPIMAndElec.ProductMOID_GetbySN(this.ClsDataProductParam.sProductSN)) == null || this.ClsDataProductParam.sMOID == string.Empty)
            {
                ErrMsg = "批号MOID不存在！";
                return false;
            }
            if ((this.ClsDataProductParam.sProcode = this.ClsPIMAndElec.ProductTypeID_GetbyMOID(this.ClsDataProductParam.sMOID)) == null || this.ClsDataProductParam.sProcode == string.Empty)
            {
                ErrMsg = "产品代码(S代码)不存在！";
                return false;
            }
            if ((this.ClsDataProductParam.sProcodeName = this.ClsPIMAndElec.ProductTypeName_GetbyProductTypeID(this.ClsDataProductParam.sProcode)) == null || this.ClsDataProductParam.sProcodeName == string.Empty)
            {
                ErrMsg = "产品类型名称不存在！";
                return false;
            }
            return true;
        }
        bool ReadPimIsPass()
        {/*
          *1、该产品代码最新的成品交调测试记录存在且pass--->return true;   FROM [ATS].[dbo].[PIMTest]
          *2、该产品代码最新的成品交调测试记录不存在或者fail,判断特采是否pass;   [ATS].[dbo].[tb_MOIDConfig]; pass-->retrun true, fail--->return false 
          */

            //         4、PimResult_GetbySN---通过SN，获取SN对应交调最近时间成品OR成品返修测试结果及交调测试最差值，输入参数(string)---SN  输出参数--测试结果PIMPass、交调测试最差值MaxPIMValue
            //5、PIMSpecail_GetbyMOID---通过MOID，获取MOID对应交调特采数据集合，输入参数(string)---MOID  输出参数--交调特采数据集合

            PIMTestInfo INFO = this.ClsPIMAndElec.PimResult_GetbySN(this.ClsDataProductParam.sProductSN);
            ClsDataSpecial SEPCIAL = this.ClsPIMAndElec.PIMSpecail_GetbyMOID(this.ClsDataProductParam.sMOID);
            ErrMsg = "该产品交调测试不合格，且特采数据不合格";
            if (INFO != null && INFO.PIMPass.ToUpper().Contains("PASS"))
            {
                return true;
            }
            if (SEPCIAL != null)
            {
                if (SEPCIAL.bEnable)
                {
                    if (!SEPCIAL.bNeedCheckPIM)
                    {
                        return true;
                    }
                    else if (INFO != null && SEPCIAL.fMinVaule <= INFO.MaxPIMValue && SEPCIAL.fMaxVaule >= INFO.MaxPIMValue)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            return false;

            /*  PIMTestInfo INFO;
            if ((INFO = this.ClsPIMAndElec.PimResult_GetbySN(this.ClsDataProductParam.sProductSN)) == null)
            {
                ErrMsg = "该产品尚未进行交调测试";
                return false;
            }
            if (INFO.PIMPass.ToUpper().Contains("PASS"))
            {
                return true;
            }
            ClsDataSpecial SEPCIAL;
            if ((SEPCIAL = this.ClsPIMAndElec.PIMSpecail_GetbyMOID(this.ClsDataProductParam.sMOID)) == null)
            {
                ErrMsg = "该产品交调测试不合格，且不存在特采数据";
                return false;
            }
            if (!SEPCIAL.bEnable)
            {
                ErrMsg = "该产品交调测试不合格，特采数据不可用";
                return false;
            }
            if (!SEPCIAL.bNeedCheckPIM)
            {
                return true;
            }
            if (SEPCIAL.fMinVaule <= INFO.MaxPIMValue && SEPCIAL.fMaxVaule >= INFO.MaxPIMValue)
            {
                return true;
            }
            else
            {
                ErrMsg = "该产品交调测试不合格，且特采数据不合格";
                return false;
            }
           * */
        }
        bool ReadElectricIsPass()
        {/*
          *1、该产品代码最新的成品电性能测试记录存在且pass--->return true, else--->return false;  FROM [ATS].[dbo].[TTest]
          */

            //6、ElectricResult_GetbySN--通过SN，获取SN对应电性能最近时间成品OR成品返修测试结果，输入参数(string)--SN,输出参数--测试结果IsPass
            if (!this.ClsPIMAndElec.ElectricResult_GetbySN(this.ClsDataProductParam.sProductSN))
            {
                ErrMsg = "电性能测试不合格";
                return false;
            }
            return true;
        }
        bool ReadRcuIsPass()
        {/*
          * 1、该产品代码RCU检测是否存在且pass--->return true, else--->return false;    FROM [ATS].[dbo].[tb_RCUStatusCheck]
          */

            //7、RcuResult_GetbySN--通过SN，获取SN对应RCU检测结果，输入参数(string)---SN,输出参数---RCU状态检测结果Status
            if (!this.ClsPIMAndElec.RcuResult_GetbySN(this.ClsDataProductParam.sProductSN))
            {
                ErrMsg = "RCU检测不合格";
                return false;
            }
            return true;
        }


        bool ReadPrintRecord()
        {
            if (this.ClsPIMAndElec.Atenna_PrintData_Download(this.ClsDataProductParam.sProductSN) == null)
            {
                return false;
            }
            return true;
        }
        bool WritePrintRecord(PrintRecordInfo record)
        {
            bool value = false;
            value = this.ClsPIMAndElec.Atenna_PrintData_Upload(record);
            if (!value)
            {
                ErrMsg = "上传数据失败";
            }
            return value;
        }

        bool GetTemple(string sProcode)
        {//查找文件路径模板  代入S代码
            if ((this.PrintInfo.browsingFormats = getTemple(sProcode)) == null)
                return false;
            else
                return true;
        }
        string[] getTemple(string sProcode)
        {//代入S代码查询标签模板
            string sPath = this.PrintInfo.sTemplePath;// @"D:\天线标签追溯系统项目\LabelTemple\";
            try
            {
                string[] browsingFormats = System.IO.Directory.GetFiles(sPath, "*.btw");
                List<string> LsbrowsingFormats = new List<string>();
                if (this.PrintInfo.TemType == (int)TempleType.procode)
                {
                    sProcode += "(代码)";
                }
                else if (this.PrintInfo.TemType == (int)TempleType.model)
                {
                    sProcode += "(型号)";
                }
                foreach (string obj in browsingFormats)
                {
                    string sobj = System.IO.Path.GetFileName(obj);
                    if (sobj.Contains(sProcode))
                    {
                        LsbrowsingFormats.Add(obj);
                    }
                }
                if (LsbrowsingFormats.Count <= 0)
                {
                    ErrMsg = string.Format("未能找到{0}对应模板,请建立好模板以--->{1}<---命名并存放到{2}目录下", sProcode, sProcode, sPath);
                    return null;
                }
                return LsbrowsingFormats.ToArray();
            }
            catch (Exception ex)
            {
                ErrMsg = ex.ToString();
            }
            return null;
        }

    }

    class clsProcessForAntennaPack
    {
        object m_handle;
        ClsConfig m_config;
        ClsPackConfigInfo m_PackConfigInfo;
        ClsPackInfo m_packinfo;
        ClsRelevanceInfo m_relevanceinfo;
        ClsServiceForPackage ClsPackage;
        ClsTProductTypeInfo ClsTProductTypeInfo_Select;
        clsProcessForCheckAntennaTest m_ProcessForCheckAntennaTest;

        public event Func<string, bool> ShowMsgevent;//数据刷新事件
        public clsProcessForAntennaPack(object m_handle, ClsConfig m_config)
        {
            this.m_handle = m_handle;
            this.m_config = m_config;
            this.ClsPackage = new ClsServiceForPackage();
            m_ProcessForCheckAntennaTest = new clsProcessForCheckAntennaTest();
            m_ProcessForCheckAntennaTest.ToSetConfig(m_config);
        }
        public List<ClsTProductTypeInfo> getTProductType()
        {
            if (ClsDifinition.isDebug)
            {
                List<ClsTProductTypeInfo> lt = new List<ClsTProductTypeInfo>();
                lt.Add(new ClsTProductTypeInfo() { ProductTypeID = "1111", IsReviewCode = 0 });
                lt.Add(new ClsTProductTypeInfo() { ProductTypeID = "2222", IsReviewCode = 1 });
                return lt;
            }
            List<ClsTProductTypeInfo> lt_ClsTProductTypeInfo = new List<ClsTProductTypeInfo>();
            lt_ClsTProductTypeInfo = ClsPackage.getProductTypeList();
            //lambda表达式 排序
            lt_ClsTProductTypeInfo.Sort((x, y) => { return x.ProductTypeID.CompareTo(y.ProductTypeID); });
            return lt_ClsTProductTypeInfo;
        }
        public ClsPackConfigInfo getPackConfig(string productTypeID)
        {
            List<ClsPackConfigInfo> lt = ClsPackage.getPackConfigList(productTypeID);
            if (lt == null || lt.Count < 1)
                return m_PackConfigInfo = null;
            return m_PackConfigInfo = lt[0];
        }
        public void ToSetting(ClsPackInfo m_packinfo, ClsRelevanceInfo m_relevanceinfo, ClsTProductTypeInfo ClsTProductTypeInfo_Select)
        {
            this.m_packinfo = m_packinfo;
            this.m_relevanceinfo = m_relevanceinfo;
            this.ClsTProductTypeInfo_Select = ClsTProductTypeInfo_Select;
        }
        public bool ToSetting(ClsPackInfo m_packinfo, ClsRelevanceInfo m_relevanceinfo)
        {
            this.m_packinfo = m_packinfo;
            this.m_relevanceinfo = m_relevanceinfo;
            if (IsLegalization(m_packinfo, m_PackConfigInfo) &&
                IsFixConsistency(m_packinfo, m_PackConfigInfo))
            {
                if (m_PackConfigInfo.RSN)
                {
                    if (IsExistReviewSN(m_packinfo.RetrospectSN))
                    {
                        (m_handle as FrmMain).Tips("追溯条码已存在,无法录入!", 0);
                        return false;
                    }
                    if (IsRelevance(m_packinfo.RetrospectSN))
                    {
                        (m_handle as FrmMain).Tips("已关联,无法录入!", 0);
                        return false;
                    }
                }
                if (m_PackConfigInfo.PSN)
                {
                    if (IsExistProductSN(m_packinfo.ProductSN))
                    {
                        (m_handle as FrmMain).Tips("天线条码已存在,无法录入!", 0);
                        return false;
                    }
                }
                if (!Test_Check())
                    return false;
                return true;
            }
            return false;
        }
        public void ToStratProcess()
        {
            string msg;
            if (UpdatePackInfo())
            {
                msg = "包装数据录入成功!\r\n";
                if (m_PackConfigInfo.RSN)
                {
                    if (UpdateRelevanceInfo())
                    {
                        msg += "包装数据关联成功!\r\n";
                    }
                    else
                    {//已录入成功,但关联失败，应自动删除已包装的信息
                        msg += "包装数据关联失败!\r\n";
                    }
                }
            }
            else
            {
                msg = "包装数据录入失败!\r\n";
            }
            ShowMsgevent(msg);
            (m_handle as FrmMain).Tips(msg, 0);
        }

        #region  pack_insert
        bool IsFixConsistency(ClsPackInfo info, ClsPackConfigInfo config)
        {
            if (config == null)
            {
                MessageBox.Show("规则尚未配置", "Err");
                return false;
            }
            int PCodeRule = config.PCodeRule;
            switch (PCodeRule)
            {
                case 1:
                case 2:
                    if (info.ProductSN == info.OutsideBSN && info.ProductSN == info.OutsideCSN)
                    {
                        return true;
                    }
                    break;
                case 3:
                case 4:
                    if (info.ProductSN == info.OutsideBSN)
                    {
                        return true;
                    }
                    break;
                case 5:
                case 6:
                    if (info.RetrospectSN == info.ProductSN && info.RetrospectSN == info.OutsideBSN)
                    {
                        return true;
                    }
                    break;
                case 7:
                    if ((info.OutsideBSN == info.OCSN1 && info.OutsideBSN == info.OCSN2 && info.OutsideBSN == info.OCSN3) ||
                        ((info.OutsideCSN == info.OCSN1 && info.OutsideCSN == info.OCSN2 && info.OutsideCSN == info.OCSN3)))
                    {
                        return true;
                    }
                    break;
                case 8:
                case 9:
                    string BTBC = info.BTBC.Substring(18, 12);
                    string TBC = info.TBC.Substring(18, 12);
                    if (info.OutsideBSN == BTBC && info.OutsideBSN == TBC)
                    {
                        return true;
                    }
                    break;
                default:
                    MessageBox.Show("规则尚未配置", "Err");
                    return false;
            }
            MessageBox.Show("不符合一致性规则==>" + PCodeRule, "Err");
            return false;
        }
        bool IsLegalization(ClsPackInfo info, ClsPackConfigInfo config)
        {
            if (config == null)
            {
                MessageBox.Show("规则尚未配置", "Err");
                return false;
            }
            if (config.RSN && !IsFixSnFormat(0, info.RetrospectSN))
                return false;
            if (config.PSN && !IsFixSnFormat(1, info.ProductSN))
                return false;
            if (config.TBC && !IsFixSnFormat(2, info.TBC))
                return false;
            if (config.OBSN && !IsFixSnFormat(3, info.OutsideBSN))
                return false;
            if (config.BTBC && !IsFixSnFormat(4, info.BTBC))
                return false;
            if (config.ALDSN && !IsFixSnFormat(5, info.ALDSN))
                return false;
            if (config.OCSN && !IsFixSnFormat(6, info.OutsideCSN))
                return false;
            if (config.OCSN1 && !IsFixSnFormat(7, info.OCSN1))
                return false;
            if (config.OCSN2 && !IsFixSnFormat(8, info.OCSN2))
                return false;
            if (config.OCSN3 && !IsFixSnFormat(9, info.OCSN3))
                return false;
            return true;
        }
        bool IsFixSnFormat(int ID, string sn)
        {
            string mes;
            switch (ID)
            {
                case 0: //追溯条码    
                    mes = "追溯条码";
                    if (Function.IsDigitOrLetter(sn, 19, 30))
                    {
                        return true;
                    }
                    break;
                case 1://天线条码
                    mes = "天线条码";
                    if (Function.IsDigitOrLetter(sn, 19, 30))
                    {
                        if (Regex.IsMatch(sn[0].ToString(), @"^(R|r)"))
                        {/*中兴代码（R0371000050030900）+年月(1710)+六位流水码（400001）
                          * 固定50030900  摩比对应中兴的客户代码
                          * 举例：1、 R03330000500309001710400001 
                          * 2、R03540100500309001711400001  
                          * 3、R03540000500309001709400001
                          */
                            if (Regex.IsMatch(sn.Substring(9, 8), @"50030900") &&
                                Regex.IsMatch(sn.Substring(16, 4), @"^\d{4}$"))
                            {
                                return true;
                            }
                        }
                        else if (Regex.IsMatch(sn.Substring(0, 2).ToUpper(), @"^(MB|JA|XA)"))
                        {/* XA +产品代码（TDA0100475)+版本号(A10)+年份（H）+周（46）+五位流水码 
                          * XATDA0100475A10H4600001
                          * XA +产品代码+年份（H）+周（46）+五位流水码     2017年"H"--2018年"I"
                          * XAT011110001H4240001
                          */
                            if (Regex.IsMatch(sn[12].ToString(), @"^[a-zA-Z]") || Regex.IsMatch(sn[15].ToString(), @"^[a-zA-Z]"))
                            {
                                return true;
                            }
                        }
                        else
                        {//TDA0200209G917100000B
                            if (sn.Substring(10, 2) == "G9")
                            {
                                return true;
                            }
                        }
                    }
                    break;
                case 2://天线二维码
                    mes = "天线二维码";
                    if (Function.IsMatchLength(sn, 30, 50))
                    {/*[)>061P474185A.101SG917410000B18VLENSN4LCN10D1741
                      *[)>061P474059A.101SG917280000118VLENSN4LCN10D1304   
                      */
                        if (sn.Substring(0, 3) == "[)>")
                        {
                            return true;
                        }
                    }
                    break;
                case 3://外箱条码
                    mes = "外箱条码";
                    if (Function.IsDigitOrLetter(sn, 12, 50))
                    {/*SG9172800001
                      *SG917410000B   
                      *XATDA0200091A10H5000007
                      *R03510100500309001801400001                     
                      */
                        return true;
                    }
                    break;
                case 4://外箱二维码
                    mes = "外箱二维码";
                    if (Function.IsMatchLength(sn, 30, 50))
                    {/*[)>061P474185A.101SG917410000BQ14LCN18VLENSN
                      *[)>061P474059A.101SG9172800001Q14LCN18VLENSN 
                      */
                        if (sn.Substring(0, 3) == "[)>")
                        {
                            return true;
                        }
                    }
                    break;
                case 5://模块ALD标签
                    mes = "模块ALD标签";
                    if (Function.IsDigitOrLetter(sn, 14, 20))
                    {/*MTAU006H460211T  MT（摩比）+AU006（料号）+H46(年周）+0211(序列码）+T（类型）
                      *MTAU020I100023   MT（摩比）+AU020（料号）+I10 (年周）+0023 (序列码）
                      */
                        if (sn.Substring(0, 2) == "MT")
                        {
                            return true;
                        }
                    }
                    break;
                case 6://外罩条码
                    mes = "外罩条码";
                    if (Function.IsDigitOrLetter(sn, 19, 30))
                    {/*R03510100500309001801400001                     
                      */
                        return true;
                    }
                    break;
                case 7://外罩条码1
                    mes = "外罩条码1";
                    if (Function.IsDigitOrLetter(sn, 19, 30))
                    {/*R03510100500309001801400001                     
                      */
                        return true;
                    }
                    break;
                case 8://外罩条码2
                    mes = "外罩条码2";
                    if (Function.IsDigitOrLetter(sn, 19, 30))
                    {/*R03510100500309001801400001                     
                      */
                        return true;
                    }
                    break;
                case 9://外罩条码3
                    mes = "外罩条码3";
                    if (Function.IsDigitOrLetter(sn, 19, 30))
                    {/*R03510100500309001801400001                     
                      */
                        return true;
                    }
                    break;
                default:
                    MessageBox.Show("条码格式规则未有匹配项", "Err");
                    return false;
            }
            MessageBox.Show(mes + "==>不符合条码格式规则", "Err");
            return false;
        }

        bool IsRelevance(string ProductSN)
        {
            if (!this.ClsPackage.IsRelevance(ProductSN)) return false;
            return true;
        }
        bool IsExistReviewSN(string ReviewSN)
        {
            if (!this.ClsPackage.IsRASN(ReviewSN)) return false;
            return true;
        }
        bool IsExistProductSN(string ProductSN)
        {
            if (!this.ClsPackage.IsASN(ProductSN)) return false;
            return true;
        }
        void DeleteExistReviewSN(string ReviewSN)
        {//删除数据库已存在的追溯条码RelevanceInfo记录

        }
        void DeleteExistProductSN(string ProductSN)
        {

        }
        bool UpdateRelevanceInfo()
        {
            if (!this.ClsPackage.UploalRelevanceData(this.m_relevanceinfo)) return false;
            return true;
        }
        bool UpdatePackInfo()
        {
            if (!this.ClsPackage.UploalPackageData(this.m_packinfo)) return false;
            return true;
        }
        #endregion

        bool Test_Check()
        {
            ClsDataProduct dtpd = new ClsDataProduct();
            if (/*ClsTProductTypeInfo_Select.IsReviewCode == (int)ClsTProductTypeInfo.emReviewCode.IsReviewCode*/ m_PackConfigInfo.RSN)
            {//有追溯条码，以追溯条码检测测试数据
                dtpd.sProductSN = m_packinfo.RetrospectSN;
            }
            else
            {//无追溯条码，以天线条码检测测试数据
                dtpd.sProductSN = m_packinfo.ProductSN;
            }

            if (!m_ProcessForCheckAntennaTest.ToSetProductParam(dtpd))
            {
                (m_handle as FrmMain).Tips(m_ProcessForCheckAntennaTest.m_Msg, 0);
                return false;
            }
            ShowMsgevent(string.Format("产品序列号:{0}\r\n产品代码:{1}\r\n批号MOID:{2}\r\n类型名称:{3}",
                dtpd.sProductSN, dtpd.sProcode, dtpd.sMOID, dtpd.sProcodeName));

            if (m_config.setModule == (int)ClsConfig.SetModule.reform)
            {
                (m_handle as FrmMain).Tips("录入信息禁止改造功能,请重设配置", 0);
                return false;
            }

            m_ProcessForCheckAntennaTest.ToSetConfig(m_config);
            if (!m_ProcessForCheckAntennaTest.Process())
            {
                (m_handle as FrmMain).Tips(m_ProcessForCheckAntennaTest.m_Msg, 0);
                return false;
            }
            ShowMsgevent(m_ProcessForCheckAntennaTest.m_Msg);

            return true;
        }
    }

    class clsProcessForCheckAntennaTest : clsCheckAntennaTest
    {
        public ClsConfig m_config;
        public void ToSetConfig(ClsConfig config)
        {
            m_config = config;
        }
        public bool Process()
        {
            if (this.m_config.setModule == (int)ClsConfig.SetModule.reform)
            {
                if (!Reform_Process())
                    return false;
            }
            else if (this.m_config.setModule == (int)ClsConfig.SetModule.uCheck)
            {
                if (!UnCheck_Process())
                    return false;
            }
            else if (this.m_config.setModule == (int)ClsConfig.SetModule.normal)
            {
                if (!Normal_Process())
                    return false;
            }
            return true;
        }
        bool Normal_Process()
        {
            bool value = false;

            if (!ReadPimIsPass(m_ProductParam))
                return value;
            else
                m_Msg = "交调测试合格\r\n";

            if (!ReadElectricIsPass(m_ProductParam.sProductSN))
                return value;
            else
                m_Msg += "电性能测试合格\r\n";

            if (!ReadRcuIsPass(m_ProductParam.sProductSN))
                return value;
            else
                m_Msg += "RCU测试合格\r\n";

            return value == false;
        }
        bool Reform_Process()
        {
            bool value = false;
            if (m_config.numReform-- > 0)
            {
                value = true;
            }
            if (m_config.numReform <= 0)
            {
                m_config.setModule = (int)ClsConfig.SetModule.normal;
            }
            return value;
        }
        bool UnCheck_Process()
        {
            bool value = false;
            bool isuPIM = this.m_config.isuPIM,
                 isuElectric = this.m_config.isuElectric,
                 isuRcu = this.m_config.isuRcu;
            int numPIM = this.m_config.numPIM,
                numElectric = this.m_config.numElectric,
                numRcu = this.m_config.numRcu;

            if (!isuPIM)
            {
                if (!ReadPimIsPass(m_ProductParam))
                {
                    return value;
                }
                else
                {
                    m_Msg = "交调测试合格\r\n";
                }
            }
            else
            {
                m_Msg = string.Format("交调免测数量剩余:{0}\r\n", --numPIM);
                if (numPIM <= 0)
                {
                    isuPIM = false;
                    m_Msg += string.Format("交调免测复位\r\n");
                }
            }

            if (!isuElectric)
            {
                if (!ReadElectricIsPass(m_ProductParam.sProductSN))
                {
                    return value;
                }
                else
                {
                    m_Msg += "电性能测试合格\r\n";
                }
            }
            else
            {
                m_Msg += string.Format("电性能免测数量剩余:{0}\r\n", --numElectric);
                if (numElectric <= 0)
                {
                    isuElectric = false;
                    m_Msg += string.Format("电性能免测复位\r\n");
                }
            }

            if (!isuRcu)
            {
                if (!ReadRcuIsPass(m_ProductParam.sProductSN))
                {
                    return value;
                }
                else
                {
                    m_Msg += "RCU测试合格\r\n";
                }
            }
            else
            {
                m_Msg += string.Format("电性能免测数量剩余:{0}\r\n", --numRcu);
                if (numRcu <= 0)
                {
                    isuRcu = false;
                    m_Msg += string.Format("RCU免测复位\r\n");
                }
            }

            this.m_config.isuPIM = isuPIM;
            this.m_config.isuElectric = isuElectric;
            this.m_config.isuRcu = isuRcu;
            this.m_config.numPIM = numPIM;
            this.m_config.numElectric = numElectric;
            this.m_config.numRcu = numRcu;
            if (this.m_config.isuPIM == false && this.m_config.isuElectric == false && this.m_config.isuRcu == false)
            {
                this.m_config.setModule = (int)ClsConfig.SetModule.normal;
                m_Msg += string.Format("免测复位\r\n");
            }
            return value == false;
        }
    }
    class clsCheckAntennaTest
    {
        public ClsDataProduct m_ProductParam;
        public ClsServicePIMAndElecForAntenna m_PIMAndElec;
        public string m_Msg = "";
        public clsCheckAntennaTest()
        {
            m_PIMAndElec = new ClsServicePIMAndElecForAntenna();
        }
        public bool ToSetProductParam(ClsDataProduct PD)
        {
            if (!ReadMatchSN(PD))
            {
                return false;
            }
            m_ProductParam = PD;
            return true;
        }
        bool ReadMatchSN(ClsDataProduct DtPd)
        {
            DtPd.sMOID = m_PIMAndElec.ProductMOID_GetbySN(DtPd.sProductSN);
            if (DtPd.sMOID == null || DtPd.sMOID == "")
            {
                m_Msg = "该产品未测试!(批号MOID不存在)";
                return false;
            }
            DtPd.sProcode = m_PIMAndElec.ProductTypeID_GetbyMOID(DtPd.sMOID);
            if (DtPd.sProcode == null || DtPd.sProcode == "")
            {
                m_Msg = "该产品未测试!(产品代码(S代码)不存在)";
                return false;
            }
            DtPd.sProcodeName = m_PIMAndElec.ProductTypeName_GetbyProductTypeID(DtPd.sProcode);
            if (DtPd.sProcodeName == null || DtPd.sProcodeName == "")
            {
                m_Msg = "该产品未测试!(产品类型名称不存在)";
                return false;
            }
            return true;
        }
        public bool ReadPimIsPass(ClsDataProduct DtPd)
        {
            PIMTestInfo INFO = m_PIMAndElec.PimResult_GetbySN(DtPd.sProductSN);
            ClsDataSpecial SEPCIAL = m_PIMAndElec.PIMSpecail_GetbyMOID(DtPd.sMOID);

            if (INFO != null && INFO.PIMPass.ToUpper().Contains("PASS"))
            {
                return true;
            }
            if (SEPCIAL != null)
            {
                if (SEPCIAL.bEnable)
                {
                    if (!SEPCIAL.bNeedCheckPIM)
                    {
                        return true;
                    }
                    else if (INFO != null && SEPCIAL.fMinVaule <= INFO.MaxPIMValue && SEPCIAL.fMaxVaule >= INFO.MaxPIMValue)
                    {
                        return true;
                    }
                    else
                    {
                        m_Msg = "交调测试不合格，且特采数据不合格";
                        return false;
                    }
                }
            }
            m_Msg = "交调测试不合格";
            return false;
        }
        public bool ReadElectricIsPass(string sn)
        {
            if (!/*m_PIMAndElec.EleResult_AllStatus_GetbySN(sn)*/ m_PIMAndElec.ElectricResult_GetbySN(sn))
            {
                m_Msg = "电性能测试不合格";
                return false;
            }
            return true;
        }
        public bool ReadRcuIsPass(string sn)
        {
            if (!m_PIMAndElec.RcuResult_GetbySN(sn))
            {
                m_Msg = "RCU测试不合格";
                return false;
            }
            return true;
        }
    }
}
